﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Data;
using System.IO;
using System.Web.Script.Serialization;
using System.Configuration;

namespace website.CustomerInfo
{
    /// <summary>
    /// CustomerHandler 的摘要说明
    /// </summary>
    public class CustomerHandler : IHttpHandler
    {
        string Action = "";
       JavaScriptSerializer jsc = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
           
            Action = RequstString("Action");
            
            //HttpPostedFile file = context.Request.Files[0];

            if (Action == "GetEquNoList")
            {
                string EquType = RequstString("EquType");
                string CustomerID = RequstString("CustomerID");
                context.Response.Write(GetEquNoList(EquType,CustomerID));
            }
            // 保存新增客户信息
            else if (Action == "addCustomInfo")
            {
                string CustomDiYu = RequstString("CustomDiYu");
                string CustomName = RequstString("CustomName");
                string CustomChejian = RequstString("CustomChejian");
                string provience = RequstString("provience");
                string city = RequstString("city");
                string area = RequstString("area");
                string CustomDetailAddress = RequstString("CustomDetailAddress");
                string CustomLng = RequstString("CustomLng");
                string CustomLat = RequstString("CustomLat");
                string CustomImg = RequstString("CustomImg");
                string customerstate = RequstString("customerstate");
                string InstallDate = RequstString("InstallDate");
                string installstate = RequstString("installstate"); 
                string Reamrk = RequstString("Reamrk");
                context.Response.Write(addCustomInfo(CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat,CustomImg,customerstate, InstallDate,installstate, Reamrk));

            }
            //保存编辑客户信息
            else if (Action == "editCustomInfo")
            {
                string CustomerID = RequstString("CustomerID");
                string CustomDiYu = RequstString("CustomDiYu");
                string CustomName = RequstString("CustomName");
                string CustomChejian = RequstString("CustomChejian");
                string provience = RequstString("provience");
                string city = RequstString("city");
                string area = RequstString("area");
                string CustomDetailAddress = RequstString("CustomDetailAddress");
                string CustomLng = RequstString("CustomLng");
                string CustomLat = RequstString("CustomLat");
                string CustomImg = RequstString("CustomImg");
                string customerstate = RequstString("customerstate");
                string InstallDate = RequstString("InstallDate");
                string installstate = RequstString("installstate");
                string Reamrk = RequstString("Reamrk");
                context.Response.Write(editCustomInfo(CustomerID,CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat, CustomImg, customerstate, InstallDate, installstate, Reamrk));

            }
            else if (Action == "GetCustomerInfo")
            {
                string sidx = RequstString("sidx");
                string sord = RequstString("sord");
                string keyvalue = RequstString("keyword");
                //string CustomerName = RequstString("CustomerName");
                context.Response.Write(GetCustomerInfo(sidx, sord, keyvalue));
            }
            //根据ID查找客户厂名   车间
            else if (Action == "getCustomNameByID")
            {
                context.Response.Write(getCustomNameByID());
            }
            //根据ID查找客户全部信息
            else if (Action == "getCustomInfoByID")
            {
                context.Response.Write(getCustomInfoByID());
            }
            else if (Action == "GetCustomerEquInfo")
            {
                string sidx = RequstString("sidx");
                string sord = RequstString("sord");
                string CustomerID = RequstString("CustomerID");
                string keyvalue = RequstString("keyword");
                //string CustomerName = RequstString("CustomerName");
                context.Response.Write(GetCustomerEquInfo(sidx, sord,CustomerID, keyvalue));
            }
            else if (Action == "uploadimage")
            {
                returnresult result = new returnresult();
                result = uploadimage();
                context.Response.Write(jsc.Serialize(result));
            }

            else if (Action == "deleteCustomerInfo")
            {
                context.Response.Write(deleteCustomerInfo());
            }
            else if (Action == "deleteCustomerEquInfo")
            {
                context.Response.Write(deleteCustomerEquInfo());
            }

            //设置客户设备投入使用时间
            else if (Action == "setCustomerUseDate")
            {
                string CustomerID = RequstString("CustomerID");
                string UseDate = RequstString("UseDate");
                context.Response.Write(setCustomerUseDate(CustomerID,UseDate));
            }
            //监听客户需要维护的设备信息
            else if (Action == "getCustomerMaintenanceInfo")
            {
                string CustomerID = RequstString("CustomerID");
                context.Response.Write(getCustomerMaintenanceInfo(CustomerID));
            }
            //监听客户发布的问题
            else if (Action == "getMyMessage")
            {
                context.Response.Write(getCustomerProblem());
            }

            else
            {
                var stream = new StreamReader(context.Request.InputStream).ReadToEnd();
                var msgdata = new JavaScriptSerializer().Deserialize<MSG>(stream);

                if (msgdata.Action == "saveCustomerEquKaoHaoInfo")
                {
                    context.Response.Write(saveCustomerEquKaoHaoInfo(msgdata.CustomerID, msgdata.EquType, msgdata.totalcount, msgdata.EquNo, msgdata.TaiHao));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public static string RequstString(string sParam)
        {
            return (HttpContext.Current.Request[sParam] == null ? string.Empty
                  : HttpContext.Current.Request[sParam].ToString().Trim());
        }

        public string GetEquNoList(string EquType, string CustomerID)
        {
            string json = EquInfoBLL.GetEquNoList(EquType,CustomerID);
            return json;
        }

        //定义一个类用来将前端传递过来的数据进行反序列化  
        public class MSG
        {
            public string Action { set; get; }
            public string CustomerID { set; get; }
            public string EquType { set; get; }
            public string totalcount { set; get; }
            public List<string> EquNo { set; get; }

            public List<string> TaiHao { set; get; }

        }  
        /// <summary>
        /// 根据ID查找客户厂名   车间
        /// </summary>
        /// <returns></returns>
        public string getCustomNameByID()
        {
            string ID = RequstString("ID");
            string json = CustomerBLL.getCustomNameByID(ID); 
            //string json = UserMenuBLL.GetMenuList();
            return json;
        }
        /// <summary>
        /// 根据ID查找客户全部信息
        /// </summary>
        /// <returns></returns>
        public string getCustomInfoByID()
        {
            string ID = RequstString("ID");
            string json = CustomerBLL.getCustomInfoByID(ID);
            return json;
        }

        /// <summary>
        /// 保存新增客户信息
        /// </summary>
        /// <param name="CustomDiYu"></param>
        /// <param name="CustomName"></param>
        /// <param name="CustomChejian"></param>
        /// <param name="provience"></param>
        /// <param name="city"></param>
        /// <param name="area"></param>
        /// <param name="CustomDetailAddress"></param>
        /// <param name="CustomLng"></param>
        /// <param name="CustomLat"></param>
        /// <param name="InstallDate"></param>
        /// <param name="Reamrk"></param>
        /// <returns></returns>
        public string addCustomInfo(string CustomDiYu,string CustomName,string CustomChejian,string provience,string city,string area,string CustomDetailAddress,string CustomLng,string CustomLat,string CustomImg,string customerstate, string InstallDate,string installstate,string Reamrk)
        {
            string json = CustomerBLL.addCustomInfo(CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat, CustomImg,customerstate, InstallDate,installstate, Reamrk);
            return json;
        }
        /// <summary>
        /// 保存编辑客户信息
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="CustomDiYu"></param>
        /// <param name="CustomName"></param>
        /// <param name="CustomChejian"></param>
        /// <param name="provience"></param>
        /// <param name="city"></param>
        /// <param name="area"></param>
        /// <param name="CustomDetailAddress"></param>
        /// <param name="CustomLng"></param>
        /// <param name="CustomLat"></param>
        /// <param name="CustomImg"></param>
        /// <param name="customerstate"></param>
        /// <param name="InstallDate"></param>
        /// <param name="installstate"></param>
        /// <param name="Reamrk"></param>
        /// <returns></returns>
        public string editCustomInfo(string CustomerID,string CustomDiYu, string CustomName, string CustomChejian, string provience, string city, string area, string CustomDetailAddress, string CustomLng, string CustomLat, string CustomImg, string customerstate, string InstallDate, string installstate, string Reamrk)
        {
            string json = CustomerBLL.editCustomInfo(CustomerID,CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat, CustomImg, customerstate, InstallDate, installstate, Reamrk);
            return json;
        }
        /// <summary>
        /// 取得客户信息列表
        /// </summary>
        /// <param name="sidx"></param>
        /// <param name="sord"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public string GetCustomerInfo(string sidx, string sord, string keyvalue)
        {
            string strJson = "";
            DataTable dt = CustomerBLL.GetCustomerInfo(sidx, sord, keyvalue); 
            if (dt != null)
            {
                string page = RequstString("page");
                //String page =Re .getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数 
                string rows = RequstString("rows");  // 取得每页显示行数，,注意这是jqgrid自身的参数 
                int totalRecord = dt.Rows.Count; // 总记录数(应根据数据库取得，在此只是模拟) 
                int totalPage = totalRecord % Convert.ToInt16(rows) == 0 ? totalRecord
                / Convert.ToInt16(rows) : totalRecord / Convert.ToInt16(rows)
                + 1; // 计算总页数 
                int index = (Convert.ToInt16(page) - 1) * Convert.ToInt16(rows); // 开始记录数 
                int pageSize = Convert.ToInt16(rows);
                strJson = "{\"page\":" + page + ",\"total\": " + totalPage + "  ,\"records\":" + dt.Rows.Count.ToString() + ",\"rows\":[";
                for (int j = index; j < pageSize + index && j < totalRecord; j++)
                {
                    strJson += "{";
                    strJson += "\"id\":\"" + (j + 1).ToString() + "\",";
                    strJson += "\"cell\":";

                    strJson += "[";
                    strJson += "\"" + dt.Rows[j]["ID"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomDiYu"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomChejian"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomProvience"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomCity"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomArea"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomDetailAddress"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomeState"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["InstallDate"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["InstallState"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["UseDate"].ToString() + "\"";
                    strJson += "]";
                    strJson += "}";
                    if (j != pageSize + index - 1 && j != totalRecord - 1)
                    {
                        strJson += ",";
                    }
                }
            }
            else
            {
                strJson = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";

            }
            strJson = strJson.Trim().TrimEnd(new char[] { ',' });
            strJson += "]}";
            return strJson;
        }

        public string saveCustomerEquKaoHaoInfo(string CustomerID, string EquType, string totalcount, List<string> EquNo, List<string> TaiHao)
        {
            string json = CustomerBLL.saveCustomerEquKaoHaoInfo(CustomerID,EquType,totalcount,EquNo,TaiHao);
            return json;
        }

        /// <summary>
        /// 客户所购设备列表
        /// </summary>
        /// <param name="sidx"></param>
        /// <param name="sord"></param>
        /// <param name="CustomerID"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public string GetCustomerEquInfo(string sidx, string sord, string CustomerID, string keyvalue)
        {
            string strJson = "";
            DataTable dt = CustomerBLL.GetCustomerEquInfo(sidx, sord,CustomerID, keyvalue);
            if (dt != null)
            {
                string page = RequstString("page");
                //String page =Re .getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数 
                string rows = RequstString("rows");  // 取得每页显示行数，,注意这是jqgrid自身的参数 
                int totalRecord = dt.Rows.Count; // 总记录数(应根据数据库取得，在此只是模拟) 
                int totalPage = totalRecord % Convert.ToInt16(rows) == 0 ? totalRecord
                / Convert.ToInt16(rows) : totalRecord / Convert.ToInt16(rows)
                + 1; // 计算总页数 
                int index = (Convert.ToInt16(page) - 1) * Convert.ToInt16(rows); // 开始记录数 
                int pageSize = Convert.ToInt16(rows);
                strJson = "{\"page\":" + page + ",\"total\": " + totalPage + "  ,\"records\":" + dt.Rows.Count.ToString() + ",\"rows\":[";
                for (int j = index; j < pageSize + index && j < totalRecord; j++)
                {
                    strJson += "{";
                    strJson += "\"id\":\"" + (j + 1).ToString() + "\",";
                    strJson += "\"cell\":";

                    strJson += "[";
                    strJson += "\"" + dt.Rows[j]["ID"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["CustomName"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["EquNo"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["EquType"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["totalcount"].ToString() + "\",";
                    strJson += "\"" + dt.Rows[j]["TaiHao"].ToString() + "\"";
                  
                    strJson += "]";
                    strJson += "}";
                    if (j != pageSize + index - 1 && j != totalRecord - 1)
                    {
                        strJson += ",";
                    }
                }
            }
            else
            {
                strJson = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";

            }
            strJson = strJson.Trim().TrimEnd(new char[] { ',' });
            strJson += "]}";
            return strJson;
        }


        public string deleteCustomerInfo()
        {
            string CustomerID = RequstString("CustomerID");

            string json = CustomerBLL.deleteCustomerInfo(CustomerID);

            return json;
        }

        public string deleteCustomerEquInfo()
        {
            string CustomerEquID = RequstString("CustomerEquID");

            string json = CustomerBLL.deleteCustomerEquInfo(CustomerEquID);

            return json;
        }
        public returnresult uploadimage()
        {
            returnresult rr = new returnresult();
            string uploadFilePath = HttpContext.Current.Server.MapPath("\\CustomerInfo\\uploadFilePath\\");
            HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["file"];
            //判断目录是否存在
            if (!Directory.Exists(uploadFilePath))
            {
                 Directory.CreateDirectory(uploadFilePath);
            }


            //保存文件的物理路径
            string saveFile = uploadFilePath + file.FileName;
            //保存图片到服务器
            try
            {
                file.SaveAs(saveFile);
                rr.path = file.FileName;
                rr.status = true;
                rr.msg = "文件上传成功";
            }
            catch (Exception)
            {
                rr.status = false;
                rr.path = "";
                rr.msg = "文件上传失败";
            }
            return rr;
        }

        /// <summary>
        /// 设置客户设备投入使用时间
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <param name="UseDate"></param>
        /// <returns></returns>
        public string setCustomerUseDate(string CustomerID, string UseDate)
        {
            string json = CustomerBLL.setCustomerUseDate(CustomerID, UseDate);
            return json;
        }

        /// <summary>
        /// 监听客户需要维护的设备信息
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public string getCustomerMaintenanceInfo(string CustomerID)
        {
            string json = CustomerBLL.getCustomerMaintenanceInfo(CustomerID);
            return json;
        }

        /// <summary>
        /// 监听客户发布的问题
        /// </summary>
        /// <returns></returns>
        public string getCustomerProblem()
        {
            return CustomerBLL.getCustomerProblem(RequstString("userno"));
        }


        public class returnresult
        {
            public string path { get; set; }
            public bool status{get;set;}
            public string msg{get;set;}
        }
    }
}